package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import constants.Constants;

import data.DateInfo;

import android.text.format.Time;

public class TimeUtils {
	public static int getCurrentYear() {
		Time t = new Time();
		t.setToNow();
		return t.year;
	}

	public static int getCurrentMonth() {
		Time t = new Time();
		t.setToNow();
		return t.month + 1;
	}

	public static int getCurrentDay() {
		Time t = new Time();
		t.setToNow();
		return t.monthDay;
	}

	public static int getTimeByPosition(int position, int originYear,
			int originMonth, String type) {
		int year = originYear, month = originMonth;
		if (position > 500) {
			for (int i = 500; i < position; i++) {
				month++;
				if (month == 13) {
					month = 1;
					year++;
				}
			}
		} else if (position < 500) {
			for (int i = 500; i > position; i--) {
				month--;
				if (month == 0) {
					month = 12;
					year--;
				}
			}
		}
		if (type.equals("year")) {
			return year;
		}
		return month;
	}

	public static int getWeekDay(String date) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
				Locale.getDefault());
		try {
			calendar.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek == 1) {
			dayOfWeek = 0;
		} else {
			dayOfWeek -= 1;
		}
		return dayOfWeek;
	}

	public static boolean isLeapYear(int year) {
		if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0) {
			return true;
		}
		return false;
	}

	public static int getDaysOfMonth(int year, int month) {
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		default:
			if (isLeapYear(year)) {
				return 29;
			}
			return 28;
		}
	}

	public static String getFormatDate(int year, int month) {
		String formatYear = year + "";
		String formatMonth = "";
		if (month < 10) {
			formatMonth = "0" + month;
		} else {
			formatMonth = month + "";
		}
		return formatYear + "-" + formatMonth + "-01";
	}

	public static String getFormatDate(int year, int month, int day) {
		String formatYear = year + "";
		String formatMonth = "";
		String formatDay = "";
		if (month < 10) {
			formatMonth = "0" + month;
		} else {
			formatMonth = month + "";
		}
		if (day < 10) {
			formatDay = "0" + day;
		} else {
			formatDay = day + "";
		}
		return formatYear + "-" + formatMonth + "-" + formatDay;
	}

	public static List<DateInfo> initCalendar(String formatDate, int month)
			throws Exception {
		int dates = 1;
		int year = Integer.parseInt(formatDate.substring(0, 4));
		int[] allDates = new int[42];
		for (int i = 0; i < allDates.length; i++) {
			allDates[i] = -1;
		}
		int firstDayOfMonth = TimeUtils.getWeekDay(formatDate);
		int totalDays = TimeUtils.getDaysOfMonth(year, month);
		for (int i = firstDayOfMonth; i < totalDays + firstDayOfMonth; i++) {
			allDates[i] = dates;
			dates++;
		}

		List<DateInfo> list = new ArrayList<DateInfo>();
		DateInfo dateInfo;
		for (int i = 0; i < allDates.length; i++) {
			dateInfo = new DateInfo();
			dateInfo.setDate(allDates[i]);
			if (allDates[i] == -1) {
				dateInfo.setRemark("");
				dateInfo.setNongliDate("");
				dateInfo.setThisMonth(false);
				dateInfo.setWeekend(false);
			} else {
				String date = TimeUtils.getFormatDate(year, month, allDates[i]);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
						Locale.getDefault());
				long time = sdf.parse(date).getTime();
				Lunar lunar = new Lunar(time);
				if (lunar.isSFestival()) {
					dateInfo.setNongliDate(lunar.getSFestivalName());
					dateInfo.setHoliday(true);
				} else {
					if (lunar.isLFestival()
							&& lunar.getLunarMonthString().substring(0, 1)
									.equals("闰") == false) {
						dateInfo.setNongliDate(lunar.getLFestivalName());
						dateInfo.setHoliday(true);
					} else {
						if (lunar.getLunarDayString().equals("初一")) {
							dateInfo.setNongliDate(lunar.getLunarMonthString()
									+ "月");
						} else {
							dateInfo.setNongliDate(lunar.getLunarDayString());
						}
						dateInfo.setHoliday(false);
					}
				}
				dateInfo.setThisMonth(true);
				int t = getWeekDay(getFormatDate(year, month, allDates[i]));
				if (t == 0 || t == 6) {
					dateInfo.setWeekend(true);
				} else {
					dateInfo.setWeekend(false);
				}
				dateInfo.setRemark("");
			}
			list.add(dateInfo);
		}

		int front = DataUtils.getFirstIndexOf(list);
		int back = DataUtils.getLastIndexOf(list);
		int lastMonthDays = getDaysOfMonth(year, month - 1);
		int nextMonthDays = 1;
		for (int i = front - 1; i >= 0; i--) {
			list.get(i).setDate(lastMonthDays);
			lastMonthDays--;
		}
		for (int i = back + 1; i < list.size(); i++) {
			list.get(i).setDate(nextMonthDays);
			nextMonthDays++;
		}
		return list;
	}
	
	public static int getLinesCount(String formatDate, int month) {
		int year = Integer.parseInt(formatDate.substring(0, 4));
		int firstDayOfMonth = TimeUtils.getWeekDay(formatDate);
		int totalDays = TimeUtils.getDaysOfMonth(year, month);
		int realTotal = (firstDayOfMonth + totalDays) % 7;
		if (realTotal != 0) {
			realTotal = (firstDayOfMonth + totalDays) / 7 + 1;
		} else {
			realTotal = (firstDayOfMonth + totalDays) / 7;
		}
		return realTotal;
	}

	public static List<DateInfo> initMyCalendar(String formatDate, int month)
			throws Exception {
		Random random = new Random();
		int dates = 1;
		int year = Integer.parseInt(formatDate.substring(0, 4));
		int[] allDates = new int[Constants.MAX_VIEWS_PER_MONTH];
		for (int i = 0; i < Constants.MAX_VIEWS_PER_MONTH; i++) {
			allDates[i] = -1;
		}
		int firstDayOfMonth = TimeUtils.getWeekDay(formatDate);
		int totalDays = TimeUtils.getDaysOfMonth(year, month);
		for (int i = firstDayOfMonth; i < totalDays + firstDayOfMonth; i++) {
			allDates[i] = dates;
			dates++;
		}

		List<DateInfo> list = new ArrayList<DateInfo>();
		DateInfo dateInfo;
		for (int i = 0; i < Constants.MAX_VIEWS_PER_MONTH; i++) {
			if(i == 28 && allDates[i] == -1){
				break;
			}
			if(i == 35 && allDates[i] == -1){
				break;
			}
				
			dateInfo = new DateInfo();
			dateInfo.setDate(allDates[i]);
			if (allDates[i] == -1) {
				//dateInfo.setRemark("");
				//dateInfo.setNongliDate("");
				dateInfo.setThisMonth(false);
				dateInfo.setWeekend(false);
				dateInfo.setHoliday(false);
				dateInfo.setStatus(7);
			} else {
				dateInfo.setThisMonth(true);
				int t = getWeekDay(getFormatDate(year, month, allDates[i]));
				if (t == 0 || t == 6) {
					dateInfo.setWeekend(true);
				} else {
					dateInfo.setWeekend(false);
				}
				dateInfo.setHoliday(false);
				//dateInfo.setRemark("");
				//dateInfo.setNongliDate("");
				dateInfo.setStatus(random.nextInt(7));
			}
			list.add(dateInfo);
		}

		int front = DataUtils.getFirstIndexOf(list);
		int back = DataUtils.getLastIndexOf(list);
		int lastMonthDays = getDaysOfMonth(year, month - 1);
		int nextMonthDays = 1;
		for (int i = front - 1; i >= 0; i--) {
			list.get(i).setDate(lastMonthDays);
			lastMonthDays--;
		}
		for (int i = back + 1; i < list.size(); i++) {
			list.get(i).setDate(nextMonthDays);
			nextMonthDays++;
		}
		return list;
	}
}
